<template>
  <view class="container">
    <!-- 顶部背景和导航 - 固定定位 -->
    <view class="fixed-header">
      <image src="/static/image/图层 1.png" class="bg-image" @load="onImageLoad"></image>
      <view class="title">
        <view class="back-arrow" @click="goBack"></view>
        <view>维修记录</view>
        <view></view>
      </view>
    </view>
    
    <!-- 页面内容区域 -->
    <view class="page-content">
      <!-- 核销码和二维码区域 - 仅订单有效时显示 -->
      <view class="qrcode" :style="{opacity: contentOpacity}" v-if="orderStatus === 'active'">
        <view class="qrcode-card">
          <view class="verification-code">
            <text class="code-label">核销码</text>
            <text class="code-value">YD2131YUY</text>
          </view>
          <view class="qrcode-image">
            <image src="/static/image/qrcode.png" class="qrcode-img"></image>
            <text class="qrcode-text">请出示二维码进行核销</text>
          </view>
        </view>
      </view>
      
      <!-- 内容区域 -->
      <view class="content" :style="{opacity: contentOpacity}">
        <!-- 维修信息卡片 -->
        <view class="card">
          <view class="card-title">维修信息</view>
          <view class="info-list">
            <view class="info-item">
              <text class="label">维修单号：</text>
              <text class="value">3535252</text>
            </view>
            <view class="info-item">
              <text class="label">维修状态：</text>
              <text class="value" :class="statusClass">{{ statusText }}</text>
            </view>
            <view class="info-item">
              <text class="label">车主姓名：</text>
              <text class="value">张</text>
            </view>
            <view class="info-item">
              <text class="label">联系方式：</text>
              <text class="value">1993829282</text>
            </view>
            <view class="info-item">
              <text class="label">维修方式：</text>
              <text class="value">上门取车</text>
            </view>
            <view class="info-item">
              <text class="label">取车位置：</text>
              <text class="value">山阳区32号</text>
            </view>
            <view class="info-item">
              <text class="label">预约日期：</text>
              <text class="value" :class="{'date-expired': orderStatus === 'cancelled'}">2022/11/10 11:00</text>
            </view>
            <view class="info-item">
              <text class="label">提交日期：</text>
              <text class="value" :class="{'date-expired': orderStatus === 'cancelled'}">2022/11/08 12:34</text>
            </view>
          </view>
        </view>
        
        <!-- 分隔线 -->
        <view class="divider"></view>
        
        <!-- 车辆信息卡片 -->
        <view class="card">
          <view class="card-title">车辆信息</view>
          <view class="info-list">
            <view class="info-item">
              <text class="label">车辆类型：</text>
              <text class="value">大客车</text>
            </view>
            <view class="info-item">
              <text class="label">车牌号：</text>
              <text class="value">豫J45323</text>
            </view>
            <view class="info-item">
              <text class="label">整车照片：</text>
              <view class="photo-placeholder">
                <image src="/static/image/camera.png" class="photo-icon"></image>
                <text class="photo-text">点击上传</text>
              </view>
            </view>
            <view class="info-item">
              <text class="label">维修部位照片：</text>
              <view class="photo-placeholder">
                <image src="/static/image/camera.png" class="photo-icon"></image>
                <text class="photo-text">点击上传</text>
              </view>
            </view>
          </view>
        </view>
        
        <!-- 按钮区域 -->
        <view class="button-area">
          <!-- 取消订单按钮 - 仅订单有效时显示 -->
          <view class="cancel-btn" @click="cancelOrder" v-if="orderStatus === 'active'">
            <text>取消订单</text>
          </view>
          
          <!-- 重新预约按钮 - 仅订单取消时显示 -->
          <view class="rebook-btn" @click="rebookOrder" v-if="orderStatus === 'cancelled'">
            <text>重新预约</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'

const contentOpacity = ref(0)
const orderStatus = ref('active') // 'active' 或 'cancelled'

// 计算状态文本
const statusText = computed(() => {
  return orderStatus.value === 'active' ? '进行中' : '已取消'
})

// 计算状态样式类
const statusClass = computed(() => {
  return orderStatus.value === 'active' ? 'status-in-progress' : 'status-cancelled'
})

// 返回上一页
const goBack = () => {
  uni.navigateBack({
    delta: 1,
    fail: (err) => {
      console.log('返回失败:', err)
      uni.switchTab({
        url: '/pages/index/index'
      })
    }
  })
}

// 取消订单
const cancelOrder = () => {
  uni.showModal({
    title: '确认取消',
    content: '确定要取消此维修订单吗？',
    success: (res) => {
      if (res.confirm) {
        // 更新订单状态为已取消
        orderStatus.value = 'cancelled'
        
        uni.showToast({
          title: '订单已取消',
          icon: 'success',
          duration: 2000
        })
      }
    }
  })
}

// 重新预约
const rebookOrder = () => {
  uni.showModal({
    title: '重新预约',
    content: '确定要重新预约此维修订单吗？',
    success: (res) => {
      if (res.confirm) {
        // 更新订单状态为进行中
        orderStatus.value = 'active'
        
        uni.showToast({
          title: '已重新预约',
          icon: 'success',
          duration: 2000
        })
      }
    }
  })
}

// 图片加载完成后显示内容
const onImageLoad = () => {
  setTimeout(() => {
    contentOpacity.value = 1
  }, 100)
}

// 页面加载完成后初始化
onMounted(() => {
  setTimeout(() => {
    contentOpacity.value = 1
  }, 300)
})
</script>

<style scoped>
.container {
  width: 100%;
  min-height: 100vh;
  background-color: #f5f5f5;
}

/* 固定头部区域 */
.fixed-header {
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
  height: 200px;
}

.bg-image {
  width: 100%;
  height: 200px;
}

.title {
  padding: 15px;
  width: 100%;
  display: flex;
  box-sizing: border-box;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 999;
  justify-content: space-between;
  align-items: center;
}

.title view:nth-child(2) {
  color: #fff;
  font-size: 18px;
  font-weight: 500;
}

.title view:nth-child(3) {
  width: 24px;
  height: 24px;
}

.back-arrow {
  width: 40px; /* 扩大点击区域 */
  height: 40px;
  position: relative;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.back-arrow::before {
  content: "";
  position: absolute;
  width: 12px;
  height: 12px;
  border-top: 2px solid #fff;
  border-left: 2px solid #fff;
  transform: rotate(-45deg);
  top: 50%;
  left: 50%;
  margin-top: -6px;
  margin-left: -6px;
  transition: all 0.3s ease;
}

/* 页面内容区域 - 优化定位和滚动 */
.page-content {
  width: 100%;
  margin-top: 100px; /* 保持与固定头部高度一致 */
  position: relative; /* 改为相对定位 */
  z-index: 1001; /* 降低层级到导航栏下方 */
  height: calc(120vh - 200px); /* 精确计算剩余高度 */
  overflow-y: auto; /* 保持滚动 */
  min-height: 0;
  -webkit-overflow-scrolling: touch; /* 优化移动端滚动体验 */
}

/* 核销码和二维码区域 - 优化显示 */
.qrcode {
  padding: 20px 15px;
  position: relative;
  z-index: 100;
  min-height: 200px; /* 增加最小高度保证内容空间 */
}

.qrcode-card {
  background-color: #fff;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  display: flex;
  flex-direction: column;
  align-items: center;
  max-height: none; /* 移除可能的高度限制 */
}

.verification-code {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 20px;
}

.code-label {
  font-size: 14px;
  color: #666;
  margin-bottom: 8px;
}

.code-value {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  letter-spacing: 2px;
}

.qrcode-image {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.qrcode-img {
  width: 180px; /* 增大尺寸 */
  height: 180px;
  margin-bottom: 10px;
  border: 1px solid #f0f0f0;
  border-radius: 8px;
  object-fit: contain; /* 保持比例不变形 */
}

.qrcode-text {
  font-size: 12px;
  color: #999;
}

/* 内容区域 */
.content {
  padding: 20px 15px;
  position: relative;
  z-index: 9999;
  transition: opacity 0.3s ease;
}

/* 卡片样式 */
.card {
  background-color: #fff;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 15px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

.card-title {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 18px;
  color: #333;
  padding-bottom: 10px;
  border-bottom: 1px solid #f0f0f0;
}

/* 信息列表 */
.info-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.info-item {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.label {
  color: #666;
  font-size: 15px;
  flex-shrink: 0;
  width: 100px;
}

.value {
  color: #333;
  font-size: 15px;
  flex: 1;
  text-align: right;
}

/* 状态样式 */
.status-in-progress {
  color: #FF9500;
  font-weight: 500;
}

.status-cancelled {
  color: #999;
  font-weight: 500;
}

/* 失效日期样式 */
.date-expired {
  color: #999 !important;
  text-decoration: line-through;
}

/* 照片占位符 */
.photo-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 80px;
  height: 80px;
  border: 1px dashed #ddd;
  border-radius: 8px;
  background-color: #f9f9f9;
}

.photo-icon {
  width: 24px;
  height: 24px;
  margin-bottom: 5px;
}

.photo-text {
  font-size: 12px;
  color: #999;
}

/* 分隔线 */
.divider {
  height: 1px;
  background-color: #eaeaea;
  margin: 20px 0;
}

/* 按钮区域 */
.button-area {
  margin-top: 30px;
}

/* 取消订单按钮 */
.cancel-btn {
  background-color: #fff;
  color: #FF3B30;
  border: 1px solid #FF3B30;
  border-radius: 8px;
  padding: 14px;
  text-align: center;
  font-size: 16px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.cancel-btn:active {
  background-color: #FFF5F5;
  transform: scale(0.98);
}

/* 重新预约按钮 */
.rebook-btn {
  background-color: #007AFF;
  color: #fff;
  border-radius: 8px;
  padding: 14px;
  text-align: center;
  font-size: 16px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.rebook-btn:active {
  background-color: #0056CC;
  transform: scale(0.98);
}
</style>